package com.cloud.rou.im.core.util;

import cn.hutool.core.lang.Singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p></p>Singleton util
 *
 * @author GaoShuangWen
 * @since 2019-10-30 11:26
 */
public class SingletonUtil {
    // save singleton
    private static Map<Class<?>, Object> pool = new ConcurrentHashMap();

    private SingletonUtil() {
    }

  /*  public static <T> T get(Class<T> clazz, Object... args) {
        T obj = (T) pool.get(clazz);
        if (null == obj) {
            obj = (T) pool.get(clazz);
            synchronized (SingletonUtil.class) {
                if (null == obj) {
                    try {
                        // call private constructor
                        Constructor<T> declaredConstructor;
                        Class<?> argsClazz[] = null;
                        if(args!=null){
                             argsClazz =new Class[args.length];
                            for (int i = 0; i < args.length; i++) {
                                System.out.println(args[i].getClass().getInterfaces().getClass().getName());
                                Class<?>[] interfaces = args[i].getClass().getInterfaces();
                                argsClazz[i]= interfaces[0];
                            }
                        }
                        declaredConstructor = clazz.getDeclaredConstructor(argsClazz);
                        declaredConstructor.setAccessible(true);
                        obj = declaredConstructor.newInstance(args);
                    } catch (Exception e) {
                        e.printStackTrace();
                        // Hand over the exception to the user
                        // user have to judge the result
                        return null;
                    }
                    pool.put(clazz, obj);
                }
            }
        }
        return obj;
    }*/


    public static <T> T get(Class<T> clazz, Object... args) {
        return Singleton.get(clazz,args);
    }

}